package com.mall.dw.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.dw.mapper.HotGoodsMapper;
import com.mall.dw.model.HotGoods;
import com.mall.dw.service.IHotGoodsService;
import com.mall.dw.util.DruidPage;
import com.mall.dw.util.TimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author Darren
 * @date 2022-10-14 9:48
 * Description：
 */
@Service
public class HotGoodsServiceImpl extends ServiceImpl<HotGoodsMapper, HotGoods> implements IHotGoodsService {
    @Override
    public List<HotGoods> topNum(Integer size) {
        return baseMapper.topNum(size);
    }

    @Override
    public DruidPage<List<HotGoods>> pageList(Integer page, Integer size) {
        // 计算偏移量
        DruidPage<List<HotGoods>> druidPage = new DruidPage<>(page, size);
        // 查询总数
        Integer count = baseMapper.selectCount(null);
        // 查询集合
        List<HotGoods> hotGoods = baseMapper.pageList(size, druidPage.getOffset());
        return druidPage.pages(hotGoods, count);
    }

    @Override
    public DruidPage<List<HotGoods>> pageListSort(Integer page, Integer size, String sort, String sortType) {
        // 计算偏移量
        DruidPage<List<HotGoods>> druidPage = new DruidPage<>(page, size, sort, sortType);
        // 查询总数
        Integer count = baseMapper.selectCount(null);
        // 查询集合
        List<HotGoods> hotGoods = baseMapper.pageListSort(druidPage);
        return druidPage.pages(hotGoods, count);
    }

    @Override
    public List<HotGoods> listByTime(Integer size, Integer hour) {
        String time = TimeUtil.beforeTime(TimeUtil.UNIT_HOUR, hour);
        return baseMapper.listByTime(size, time);
    }

    @Override
    public List<HotGoods> listByTimeAndExclude(Integer size, Integer hour, String[] urls) {
        String time = TimeUtil.beforeTime(TimeUtil.UNIT_HOUR, hour);
        return baseMapper.listByTimeAndExclude(size, time, StringUtils.join(urls, "','"));
    }

    @Override
    public List<Map<String, String>> searchHotGoods(Integer size, Integer hour, String[] urls, Integer max) {
        String time = TimeUtil.beforeTime(TimeUtil.UNIT_HOUR, hour);
        return baseMapper.searchHotGoods(size, time, StringUtils.join(urls, "','"), max);
    }
}
